package com.panshi.utils;

import io.swagger.annotations.Api;
import lombok.Data;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;

@Data
@Api("时间转换属性类")
public class TimeBean implements Serializable {

        private final static long minute = 60 * 1000;// 1分钟
        private final static long hour = 60 * minute;// 1小时
        private final static long day = 24 * hour;// 1天
        private final static long month = 31 * day;// 月
        private final static long year = 12 * month;// 年
        private final static long sevenday = 30 * day;// 7tian


        /**
         * 返回文字描述的日期
         *
         * @param date
         * @return
         */
        public static String getTimeFormatText(Date date) {

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

            if (date == null) {
                return null;
            }
            long diff = new Date().getTime() - date.getTime();
            long r = 0;

            if (diff > sevenday){
                return sdf.format(date);
            }
            if (diff > day) {
                r = (diff / day);
                return r + "天前";
            }
            if (diff > hour) {
                r = (diff / hour);
                return r + "个小时前";
            }
            if (diff > minute) {
                r = (diff / minute);
                return r + "分钟前";
            }
            return "刚刚";
        }

}
